package com.hb.trade.api.routine.controller.project.order;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.hb.core.pojo.enums.InvoiceTypeEnum;
import com.hb.core.pojo.project.order.dto.GetInvoiceInfoDTO;
import com.hb.core.pojo.project.order.dto.InvoiceDTO;
import com.hb.core.pojo.project.order.dto.InvoiceSubmitInfoDTO;
import com.hb.core.pojo.project.order.vo.InvoiceSubmitInfoVO;
import com.hb.trade.client.project.InvoiceClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.model.msg.ResponseDto;
import org.fzzn.core.util.ResultUtil;
import org.fzzn.core.web.anno.Version;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 发票信息
 *
 * @author jinhaolin
 * @date 2022/4/1 0001 16:58
 **/
@Slf4j
@Version(1.0)
@Api(tags = {"发票信息"})
@RestController
@RequestMapping("/{version}")
public class ApiInvoiceController {

    protected final InvoiceClient invoiceClient;

    public ApiInvoiceController(InvoiceClient invoiceClient) {
        this.invoiceClient = invoiceClient;
    }

    @ApiOperation(value = "平台开具增值税电子普通发票", notes = "平台开具增值税电子普通发票", httpMethod = "POST")
    @PostMapping("/invoice/issueOrdinaryElectronicInvoice")
    public ResponseDto issueOrdinaryElectronicInvoice(@RequestBody InvoiceSubmitInfoDTO invoiceSubmitInfoDTO) {
        Message<Boolean> result = invoiceClient.issueOrdinaryElectronicInvoice(invoiceSubmitInfoDTO);
        if (result.isFail()){
            return ResultUtil.fail(result.getMessage());
        }

        return ResultUtil.msg(result);
    }

    @ApiOperation(value = "平台开具增值税电子专用发票", notes = "平台开具增值税电子专用发票", httpMethod = "POST")
    @PostMapping("/invoice/issueDedicatedElectronicInvoice")
    public ResponseDto issueDedicatedElectronicInvoice(@RequestBody InvoiceSubmitInfoDTO invoiceSubmitInfoDTO) {
        Message<Boolean> result = invoiceClient.issueDedicatedElectronicInvoice(invoiceSubmitInfoDTO);
        if (result.isSuccess()) {
            return ResultUtil.msg(true);
        }
        return ResultUtil.FAIL;
    }

    @ApiOperation(value = "发票详情（提交信息详情）", notes = "发票详情（提交信息详情）", httpMethod = "POST")
    @PostMapping("/invoice/submitInformation")
    public ResponseDto<InvoiceSubmitInfoVO> submitInformation(@RequestBody GetInvoiceInfoDTO getInvoiceInfoDTO) {
        Message<InvoiceDTO> message = invoiceClient.submitInformation(getInvoiceInfoDTO);
        if (message.isFail()) {
            return ResultUtil.msg(false, null);
        }
        InvoiceDTO invoiceDTO = message.getData();

        // VO 转换格式，可封装
        InvoiceSubmitInfoVO invoiceSubmitInfoVO = new InvoiceSubmitInfoVO();
        invoiceSubmitInfoVO.setSubmitTime(DateUtil.format(invoiceDTO.getSubmitTime(), "yyyy-MM-dd HH:mm"));
        invoiceSubmitInfoVO.setInvoiceTypeName(InvoiceTypeEnum.getNameByLabel(invoiceDTO.getInvoiceType()));
        invoiceSubmitInfoVO.setInvoiceType(invoiceDTO.getInvoiceType());
        invoiceSubmitInfoVO.setPurchaserName(invoiceDTO.getPurchaserName());
        invoiceSubmitInfoVO.setPurchaserIdentification(invoiceDTO.getPurchaserIdentification());
        invoiceSubmitInfoVO.setPurchaserAddress(invoiceDTO.getPurchaserAddress());
        invoiceSubmitInfoVO.setPurchaserPhone(invoiceDTO.getPurchaserPhone());
        invoiceSubmitInfoVO.setPurchaserBankAccount(invoiceDTO.getPurchaserBankAccount());
        return ResultUtil.msg(true, invoiceSubmitInfoVO);
    }

    @ApiOperation(value = "下载发票", notes = "下载发票", httpMethod = "POST")
    @PostMapping("/invoice/invoicePdf")
    public ResponseDto<InvoiceSubmitInfoVO> invoicePdf(@RequestBody GetInvoiceInfoDTO getInvoiceInfoDTO) {
        InvoiceSubmitInfoVO invoiceSubmitInfoVO = new InvoiceSubmitInfoVO();
        Message<InvoiceDTO> message = invoiceClient.invoicePdf(getInvoiceInfoDTO);
        if (message.isFail()) {
            return ResultUtil.fail( message.getMessage());
        }
        InvoiceDTO data = message.getData();
        BeanUtil.copyProperties(data,invoiceSubmitInfoVO);
        return ResultUtil.msg(true,invoiceSubmitInfoVO);
    }


}
